( function ($){

/*  TODO: write some plugin code ... */

	/* This makes our musicPlayer function accessable 
	 * like any other jquery function:
	 * e.g. $("#mydivid").musicPlayer(playlist) */
	$.fn.musicPlayer = function(playlist, options){
		/* loop through all musicPlayer elements givin 
		 * this refers to the elements (the whole collection of elements)  */ 
		return this.each( function(){
			/* here inside the context this refers 
			 * to the element in each iteration */
			var defaultOptions = {
				autoplay: true,
				errorMsg: "Undskyld kan spille sangen!",
				playerTitle: "The latest and greatest music player"
				};
				
			var realOptions = $.extend(defaultOptions, options); 
			
			
			/* setting some default settings */
			var audio = $("#audioPlayer").get(0); /* getting the audio */
			audio.src = $("#song1").attr("href"); /* setting the first song as the default */
			$("#song1").addClass("playing-song"); /* setting a css class so that this li is the current song */
			audio.volume = realOptions.volume;
			if (realOptions.autoplay)
				audio.play();
			$("#player-title span").text(realOptions.playerTitle);
			
			$("#volume").hide();
			
				
			/* when a user selects from playlist */	
			$("#playlist li a").live("click", function(){
				 playSong(this);
			});
			
			/* hover over buttons - changing the css in buttons */
			$("#control-buttons li").mouseenter(function() {
				$(this).addClass('ui-state-hover');
			});
			
			/* event for mouseleave - changing the css back to regular */
			$("#control-buttons li").mouseleave(function() {
				$(this).removeClass("ui-state-hover");
			});
			
			/* event for  play-button  */
			$("#control-buttons li .ui-icon-play").live( "click", function() {
				$(this).removeClass("ui-icon-play"); /* remove play icon */
				$(this).addClass("ui-icon-pause"); /* setting the pause icon instead */
				audio.play();
			});	
			
			/* event for pause-button */
			$("#control-buttons li .ui-icon-pause").live( "click", function() {
				$(this).removeClass("ui-icon-pause"); /*removing pause icon */
				$(this).addClass("ui-icon-play"); /* setting the play icon instead */
				audio.pause();
			});				
			
			/* event for mute-button */
			$("#control-buttons li .ui-icon-volume-on").live( "click", function() {
				$(this).removeClass("ui-icon-volume-on");
				$(this).addClass("ui-icon-volume-off");
				audio.volume = 0.0;
			});	
			
			/* event for next-button */
			$("#control-buttons li .ui-icon-seek-next").live( "click", function() {
				var n = $("#player-playlist li a.playing-song");
				
				/* checking if song is the last on list */
				if ($(n).attr("id") != ("song" + playlist.length) )
					n = $(n).parent().next("li").children();
				else
					n = $("#song1"); /* then get the first song */				
					
				playSong(n);
			});				
			
			/* event for prev-button */
			$("#control-buttons li .ui-icon-seek-prev").live( "click", function() {
				var n = $("#player-playlist li a.playing-song");
				
				/* checking if song is the first on list */
				if ($(n).attr("id") != ("song1") )
					n = $(n).parent().prev("li").children();
				else
					n = $( ("#song" + playlist.length) ); /* then get the last song */				
				
				playSong(n);	
			});	
			
			/* mute on/off and */
			$("#control-buttons li .ui-icon-volume-off").live("click", function() {
				$(this).removeClass("ui-icon-volume-off");
				$(this).addClass("ui-icon-volume-on");
				audio.volume = Number($("#volume").val())/100;
			});	

			$("#control-buttons li .ui-icon-volume-on").mouseenter(function() {
				$("#volume").show();
			});			
			
			$("#volume").mouseleave(function(){
				$("#volume").hide();
			});
			
			$("#volume input").change(function() {
				audio.volume = Number(this.value)/100;
			});		
			
			/* event for song ended then play next song */
			$("#audioPlayer").bind("ended", function(){
				var n = $("#player-playlist li a.playing-song");
				
				/* checking if song is the last on list */
				if ($(n).attr("id") != ("song" + playlist.length) )
					n = $(n).parent().next("li").children();
				else
					n = $("#song1"); /* then get the first song */	
				
				playSong(n);
			});
			
			/* mostly borrowed from http://neutroncreations.com/blog/building-a-custom-html5-audio-player-with-jquery/ */
			$(audio).bind('timeupdate', function() {
				var rem = parseInt(audio.duration - audio.currentTime, 10),
				pos = (audio.currentTime / audio.duration) * 100,
				mins = Math.floor(rem/60,10),
				secs = rem - mins*60;
				
				$("#progress").next().text(mins + ':' + (secs > 9 ? secs : '0' + secs));
				$("#progress").val(pos);
				//alert (rem);
				/* 
				timeleft.text('-' + mins + ':' + (secs > 9 ? secs : '0' + secs));
				if (!manualSeek) { positionIndicator.css({left: pos + '%'}); }
				if (!loaded) {
					loaded = true;
						
					$('.player #gutter').slider({
					  value: 0,
					  step: 0.01,
					  orientation: "horizontal",
					  range: "min",
					  max: audio.duration,
					  animate: true,          
					  slide: function() {             
						manualSeek = true;
					  },
					  stop:function(e,ui) {
						manualSeek = false;         
						audio.currentTime = ui.value;
					  }
					});
				  }
				*/
			});
				
			$(audio).bind('progress', function() {
					if ((audio.buffered != undefined) && (audio.buffered.length != 0)) {
						var loaded = parseInt(((audio.buffered.end(0) / audio.duration) * 100), 10);
						$("#progress").val(loaded);
					}
			});
			
			
			$("#progress").change(function() {
				audio.currentTime = (Number(this.value)/100) * audio.buffered.end(0) ;
			});
				
			/* helper function for play from playlist, next and prev buttons */
			function playSong(el){
				var text = $(el).attr("href"); /* getting the url of song */
				$("#player-status marquee").text("now playing: " + text ); /* changing the display status */
				$(".playing-song").removeClass("playing-song");  /* removing css class from last playing song */
				$(el).addClass("playing-song"); /* setting a css class so that this li is the current song */
				$("#control-buttons li .ui-icon-play").removeClass("ui-icon-play").addClass("ui-icon-pause"); /* remove play icon and set pause icon */
				audio.src = text;
				audio.play();
			}
			
			/* helper function for rendering the playlist 
			 * input an url returns the filename */ 
			function getFileNameFromUrl(url){
				var i = url.lastIndexOf("/"); 
				return url.substr(i);
			}
		});
	}
/*	
	var altColorBool = 0;
	var arrCol = new Array();
	arrCol.push("#444");
	arrCol.push("#999");
	
	function getAltColor()
	{	
		altColorBool = !altColorBool;
		return arrCol[altColorBool];
	}
	

*/	
})(jQuery);